package com.sun.mvc.controller;

import com.sun.mvc.bean.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

@Controller
@RequestMapping(value = "/test")
public class TestController {

    ///////////////////////////////////// @RequestMapping 注解学习 begin ///////////////////////////////
    @RequestMapping(value = "/test1")
    public String test1(){
        return "target";
    }

    @RequestMapping(value = {"/test2-1","test2-2"})
    public String test2(){
        return "target";
    }

    @RequestMapping(value = "/test3",method = {RequestMethod.GET,RequestMethod.POST})
    public String test3(){
        return "target";
    }

    @RequestMapping(value = "/test4",method = RequestMethod.GET,params = {"param1","!param2","param3=3","param4!=4"})
    public String test4(){
        return "target";
    }

    @RequestMapping(value = "/test5",
            method = RequestMethod.GET,
            params = {"param1","!param2","param3=3","param4!=4"},
            headers = {"Accept","!header","Host=localhost:8080","sec-ch-ua-platform!=\"Linux\""})
    public String test5(){
        return "target";
    }

    @RequestMapping(value = "/test?6/testAnt")
    public String test6(){
        return "target";
    }

    @RequestMapping(value = "/test*6-1/testAnt")
    public String test6_1(){
        return "target";
    }

    @RequestMapping(value = "/test6-2/**/testAnt")
    public String test6_2(){
        return "target";
    }

    @RequestMapping(value = "/test7/{id}/{userName}")
    public String test7(@PathVariable(value = "id")int id,@PathVariable(value = "userName")String userName){
        System.out.println("id = " + id+" userName = "+userName);
        return "target";
    }
    ///////////////////////////////////// @RequestMapping 注解学习 end ///////////////////////////////

    ///////////////////////////////////// 获取请求参数 begin ///////////////////////////////
    // 通过Servlet原生API获取请求参数
    @RequestMapping(value = "/testServletAPI")
    public String testServletAPI(HttpServletRequest request){
        String age = request.getParameter("age");
        String userName = request.getParameter("userName");
        System.out.println("age="+age+": userName= "+userName);
        HttpSession session = request.getSession();
        System.out.println("session = " + session);
        System.out.println("session.getId() = " + session.getId());
        System.out.println("session.getAttributeNames() = " + Arrays.asList(session.getAttributeNames()));
        return "param";
    }

    // 通过控制器方法的形参获取请求参数
    @RequestMapping(value = "/testFormalParam")
    public String testFormalParam(int age,String userName,String hobby,String[] test){
        System.out.println("age="+age+": userName= "+userName);
        System.out.println("hobby = " + hobby);
        System.out.println("test = " + Arrays.asList(test));
        return "param";
    }

    // @RequestParam
    @RequestMapping(value = "/testRequestParam")
    public String testRequestParam(int age, @RequestParam(value = "user_name",required = false,defaultValue = "liSi") String userName, String hobby, String[] test){
        System.out.println("age="+age+": userName= "+userName);
        System.out.println("hobby = " + hobby);
        System.out.println("test = " + Arrays.asList(test));
        return "param";
    }

    // @RequestHeader,获取请求头信息，并把获取到的请求头信息传递给控制器方法的形参
    @RequestMapping(value = "testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept",required = false,defaultValue = "xxx") String accept,
                                    @RequestHeader(value = "Referer",required = false)String from,
                                    @RequestHeader(value = "Cookie")String cookie,
                                    HttpServletRequest request){
        System.out.println("accept = " + accept);
        System.out.println("from = " + from);
        System.out.println("cookie = " + cookie);
        System.out.println("request.getContextPath() = " + request.getContextPath());
        System.out.println("request.getServletPath() = " + request.getServletPath());
        System.out.println("request.getPathInfo() = " + request.getPathInfo());
        System.out.println("request.getPathTranslated() = " + request.getPathTranslated());
        System.out.println("request.getCookies() = " + Arrays.asList(request.getCookies()));
        return "param";
    }

    // @CookieValue
    @RequestMapping(value = "/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID")String cookie, HttpServletRequest request){
        System.out.println("request.getCookies() = " + Arrays.asList(request.getCookies()));
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println("cookie1.getName() = " + cookie1.getName());
            System.out.println("cookie1.getValue() = " + cookie1.getValue());
            System.out.println("cookie1.getDomain() = " + cookie1.getDomain());
            System.out.println("cookie1.getPath() = " + cookie1.getPath());
            System.out.println("cookie1.getMaxAge() = " + cookie1.getMaxAge());
            System.out.println("cookie1.getSecure() = " + cookie1.getSecure());
            System.out.println("cookie1.getVersion() = " + cookie1.getVersion());
        }
        System.out.println("cookie = " + cookie);
        return "param";
    }

    @RequestMapping(value = "/testPOJO")
    public String testPOJO(Person person){
        System.out.println("person = " + person);
        return "param";
    }
    ///////////////////////////////////// 获取请求参数 end ///////////////////////////////


    // 测试SpringMVC中的拦截器
    @RequestMapping(value = "/testInterceptor",method = RequestMethod.POST)
    public String testInterceptor(){
        return "success";
    }

    // 测试SpringMVC中的自定义异常处理
    @RequestMapping(value = "/testExceptionHandler",method = RequestMethod.GET)
    public String testExceptionHandler(){
        int i = 1/0;
        return "success";
    }

    // 测试SpringMVC中的自定义异常处理
    @RequestMapping(value = "/testExceptionHandler1",method = RequestMethod.GET)
    public String testExceptionHandler1(){
        int[] arr = new int[3];
        System.out.println(arr[4]);
        return "success";
    }
}
